package team.bluepen.supermarket.web.base;

import team.bluepen.supermarket.constant.Constant;
import team.bluepen.supermarket.constant.Role;
import team.bluepen.supermarket.data.entity.User;
import team.bluepen.supermarket.util.ServletUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 数据接口Servlet
 * @author Kai
 */
public abstract class DataServlet extends BaseServlet {
    protected String requestSource;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        requestSource = ServletUtil.getRefererUrl(req);
    }

    protected boolean checkUserRole(HttpServletRequest req) {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute(Constant.SESSION_USER);
        return user != null && user.getRole() != null && user.getRole() != Role.GUEST;
    }

}
